package client.material;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MaterialEditDialog extends JDialog {
    
    private JTextField nameField;
    private JTextField specificationField;
    private JTextField stockField;
    private JTextField unitField;
    private JTextField remarkField;
    private boolean confirmed = false;
    
    public MaterialEditDialog(Long id, String name, String specification, Long stock, String unit, String remark) {
        setTitle("修改物料信息");
        setModal(true);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(10, 10));
        
        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(5, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 物料名称
        formPanel.add(new JLabel("物料名称:"));
        nameField = new JTextField(name);
        formPanel.add(nameField);
        
        // 规格型号
        formPanel.add(new JLabel("规格型号:"));
        specificationField = new JTextField(specification);
        formPanel.add(specificationField);
        
        // 数量
        formPanel.add(new JLabel("数量:"));
        stockField = new JTextField(stock.toString());
        formPanel.add(stockField);
        
        // 单位
        formPanel.add(new JLabel("单位:"));
        unitField = new JTextField(unit);
        formPanel.add(unitField);
        
        // 备注
        formPanel.add(new JLabel("备注:"));
        remarkField = new JTextField(remark);
        formPanel.add(remarkField);
        
        add(formPanel, BorderLayout.CENTER);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");
        
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (validateInput()) {
                    confirmed = true;
                    dispose();
                }
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private boolean validateInput() {
        if (nameField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "物料名称不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (specificationField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "规格型号不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        try {
            Integer.parseInt(stockField.getText().trim());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "数量必须是整数！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (unitField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "单位不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public boolean isConfirmed() {
        return confirmed;
    }
    
    public String getMaterialName() {
        return nameField.getText().trim();
    }
    
    public String getSpecification() {
        return specificationField.getText().trim();
    }
    
    public Long getStock() {
        return Long.parseLong(stockField.getText().trim());
    }
    
    public String getUnit() {
        return unitField.getText().trim();
    }
    
    public String getRemark() {
        return remarkField.getText().trim();
    }
    
}
